package com.elcl.util;

import java.io.File;
import java.io.IOException;


/**
 * @author LCL
 * 2014-12-29
 * 文件的操作
 */
public class FileUtils {
	public static final String MUSIC = ".mp3";
	public static final String PIC = ".png";
	public static final String VIDEO = ".mp4";

	public static final int UNIT_BIT = 1;
	public static final int UNIT_KB = UNIT_BIT * 1024;
	public static final int UNIT_MB = UNIT_KB * 1024;
	public static final int UNIT_GB = UNIT_MB * 1024;

	/**
	 * 将某个文件夹中某种文件类型查出来
	 */
	public static void readTypeFile(File file, String tailType) {// TODO
																	// 将查找出的文件放到那
		if (!file.isHidden()) {
			if (file.isFile()) {
				if (file.getName().endsWith(tailType)) {

				}
			}
		}
	}

	/**
	 * 2014-12-29 LCL
	 * 
	 * @param file
	 * @return 获取文件大小(单位是bit)
	 */
	public static long getFileSize(File file) {
		long len = 0;
		if (!file.isHidden()) {
			if (file.isFile()) {
				len = len + file.length();
			} else {
				File[] ff = file.listFiles();
				if (null != ff && ff.length > 0) {
					for (File f : ff) {
						len = len + getFileSize(f);
					}
				}
			}
		}
		return len;
	}

	/**
	 * 2014-12-29 LCL
	 * 
	 * @param file
	 * @return 获取文件的大小(经过格式化的文件大小)
	 */
	public static String getFormatFileSize(File file) {
		long size = getFileSize(file);
		String formatSize = formatFileSize(size);
		return formatSize;
	}

	/**
	 * 2014-12-29 LCL
	 * @return
	 * 根据文件的大小，返回标准格式的文件大小格式，默认单位是bit
	 */
	public static String formatFileSize(long len) {
		return formatSize(len);
	}

	/**
	 * 2014-12-29 LCL
	 * @return
	 * 根据文件的大小，返回标准格式的文件大小格式
	 * lenUnit，是传入参数的格式大小
	 */
	public static String formatFileSize(long len, int lenUnit) {
		len = len * lenUnit;
		return formatSize(len);
	}

	/**
	 * 2014-12-29 LCL
	 * @param len
	 * @return
	 * 格式化单位，传入的单位是bit
	 */
	public static String formatSize(long len) {
		String lenStr = "";
		if (len == 0){
			return "0M";
		}else{
			if (len < UNIT_KB) {
				lenStr = len + "b";
			} else if (len < UNIT_MB) {
				lenStr = len / UNIT_KB + "kb";
			} else if (len < UNIT_GB) {
				lenStr = CommomUtils.formatNumber((double) len / (UNIT_MB), 1) + "M";
			} else if (len < UNIT_GB * 1024) {
				lenStr = CommomUtils.formatNumber((double) len / (UNIT_GB), 1) + "G";
			}
		}
		return lenStr;
	}

	/**
	 * 2014-12-29 LCL
	 * @param file
	 */
	public static void deleteFile(File file) {
		if (file.exists()) {
			if (file.isFile()) {
				file.delete();
			} else if (file.isDirectory()) {
				File files[] = file.listFiles();
				for (int i = 0, len = files.length; i < len; i++) {
					deleteFile(files[i]);
				}
			}
			file.delete();// 删除文件夹的话，必须确保他的子目录为空的
		}
	}

	public static File updateDir = null;
	public static File updateFile = null;
	public static long fileSize = 0;//文件的大小
	public static boolean isCreateFileSucess;

	/**
	 * 方法描述：createFile方法
	 */
	public static boolean createFile(String downloadFilePath, String fileName) {
		if (android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())) {
			isCreateFileSucess = true;

			updateDir = new File(downloadFilePath);
			updateFile = new File(downloadFilePath + File.separator + fileName);

			if (updateFile.exists()) {
				fileSize = updateFile.length();
				return true;
			} else {
				if (!updateDir.exists()) {
					updateDir.mkdirs();
				}
				try {
					updateFile.createNewFile();
				} catch (IOException e) {
					isCreateFileSucess = false;
					e.printStackTrace();
				}
				return false;
			}

		} else {
			isCreateFileSucess = false;
			return false;
		}
	}

	/**
	 * 方法描述：createFile方法
	 */
	public static boolean createApkFile(String downloadFilePath, String fileName) {
		return createFile(downloadFilePath, fileName + ".apk");
	}


}
